package inits

import (
	"DataSrv/data"
	"Gin/global/model"
	"UserSrv/user"
	"ViewSrv/view"
	"VipSrv/vip"
	"flag"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
)

func Init() {
	ServeConn()
}

func ServeConn() {
	DataConn()
	ViewConn()
	UserConn()
	VipConn()
}

func VipConn() {
	flag.Parse()
	// Set up a connection to the server.
	conn, err := grpc.NewClient("127.0.0.1:50054", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	model.VipClient = vip.NewVipClient(conn)
	fmt.Println("VipClient is connected")
}

func UserConn() {
	flag.Parse()
	// Set up a connection to the server.
	conn, err := grpc.NewClient("127.0.0.1:50052", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	model.UserClient = user.NewUserClient(conn)
	fmt.Println("UserClient is connected")
}

func ViewConn() {
	flag.Parse()
	// Set up a connection to the server.
	conn, err := grpc.NewClient("127.0.0.1:50053", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	model.ViewClient = view.NewViewClient(conn)
	fmt.Println("ViewClient is connected")
}

func DataConn() {
	flag.Parse()
	// Set up a connection to the server.
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	model.DataClient = data.NewDataClient(conn)
	fmt.Println("DataClient is connected")

}
